package com.kay.controller;

import com.kay.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("login")
public class LoginController {
    private Map<String, User> userMap = new HashMap<>();

    public LoginController() {
        User admin = new User("admin", "admin");
        userMap.put("admin", admin);

        User kay = new User("kay", "kay");
        userMap.put("kay", kay);
    }

    @RequestMapping("toLogin")
    public String toLogin() {
        System.out.println("================++++++++++++++跳转登录页面");
        return "login";
    }

    @RequestMapping("loginSystem")
    public String loginSystem(String username, String password, HttpSession session) {
        String redirect = "redirect:/login/toLogin";

        if (userMap != null) {
            User user = userMap.get(username);
            if (user != null && user.getPassword().equals(password)){
                redirect = "redirect:/demo/result";
                System.out.println("用户登陆成功");
                session.setAttribute("username", username + System.currentTimeMillis());
            }
        }

        return redirect;
    }
}
